#include "iis_test.h"
#include <math.h>
#include "main.h"
#include "wav.h"




char *wav_file_name="/sdcard/shanqiu.wav";

wavctrl WaveCtrlData;
FILE* Wave_File;

void WAV_PLAY_TASK(void *pvParameter)
{
    uint8_t ret=0;
    uint32_t time=0;
    uint8_t read_buf[1024];
    uint32_t read_len=0;
    size_t i2s_bytes_write = 0;
         
    Wave_File = fopen(wav_file_name,"r");	 
    if(Wave_File==NULL)
    {
        iis_test_DEBUG("打开文件失败\r\n");

        goto exit;
        return 2;
    }else
    {
       iis_test_DEBUG("打开文件成功\r\n");     
    }
    
    ret = fseek(Wave_File,WaveCtrlData.datastart,SEEK_SET);		//跳过文件头
    if(ret)
    {
        iis_test_DEBUG("偏移WAV头失败%d\r\n",ret);

        goto exit;
        return 3;
    }else
    {
        iis_test_DEBUG("偏移WAV头成功%d\r\n",WaveCtrlData.datastart);
    }
    
	WM8978_HPvol_Set(40,40);	//耳机音量设置
	WM8978_SPKvol_Set(40);		//喇叭音量设置

	WM8978_ADDA_Cfg(1,0);	//开启DAC
	WM8978_Input_Cfg(0,0,0);//关闭输入通道
	WM8978_Output_Cfg(1,0);	//开启DAC输出  

    if(WaveCtrlData.bps==16)   
	    WM8978_I2S_Cfg(2,0);	//飞利浦标准,16位数据长度
    else if(WaveCtrlData.bps==24) 
        WM8978_I2S_Cfg(2,2);	//飞利浦标准,24位数据长度
    else if(WaveCtrlData.bps==32) 
        WM8978_I2S_Cfg(2,3);	//飞利浦标准,32位数据长度

    i2s_set_clk(I2S_NUM, WaveCtrlData.samplerate, WaveCtrlData.bps, WaveCtrlData.nchannels);
    iis_test_DEBUG("开始播放\r\n");
    //xEventGroupSetBits(SYSTEM_State.Wav_event_group, BIT0);
    while(1)
    {
        //read_len = wav_buffill(Wave_File,read_buf,1024,WaveCtrlData.bps);
        read_len = FillWaveFileBuf(Wave_File,read_buf,1024,WaveCtrlData.nchannels,WaveCtrlData.bps);

        //iis_test_DEBUG("读取字节数:%d\r\n",read_len);    
        i2s_write(I2S_NUM, read_buf, read_len, &i2s_bytes_write, 100);

        //iis_test_DEBUG("写入字节数:%d\r\n",i2s_bytes_write);
        if(read_len<1024)
        {
            fclose(Wave_File);
            iis_test_DEBUG("读取结束关闭文件\r\n");
            break;
        }
        //vTaskDelay(5/portTICK_RATE_MS);
        if(++time>200)
        {
            time=0;
            wave_get_curtime(Wave_File,&WaveCtrlData);
            iis_test_DEBUG("播放进度:%02d:%02d/%02d:%02d\r\n",WaveCtrlData.cursec/60,WaveCtrlData.cursec%60,WaveCtrlData.totsec/60,WaveCtrlData.totsec%60);
            
        }
        //vTaskDelay(5/portTICK_RATE_MS);
    }

exit:
    vTaskDelete(NULL);
}


void iis_test(void)
{
    uint8_t ret=0;

/*    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_TX|I2S_MODE_RX,                                  // Only TX
        .sample_rate = 32000,
        .bits_per_sample = 16,
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           //2-channels
        .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
        .dma_buf_count = 2,
        .dma_buf_len = 1024,
        .use_apll = false,
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL2                                //Interrupt level 1
    };
    i2s_pin_config_t pin_config = {
        .bck_io_num = I2S_BCK_IO,
        .ws_io_num = I2S_WS_IO,
        .data_out_num = I2S_DO_IO,
        .data_in_num = I2S_DI_IO                                               //Not used
    };
    i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
    i2s_set_pin(I2S_NUM, &pin_config);


    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
    WRITE_PERI_REG(PIN_CTRL, READ_PERI_REG(PIN_CTRL) & 0xFFFFFFF0);
*/
    ret =  wave_decode_init(wav_file_name,&WaveCtrlData);
    if(ret)
    {
        iis_test_DEBUG("WAV初始化失败%d\r\n",ret);

        return 1;
    }


    xTaskCreate(WAV_PLAY_TASK,"WAV_PLAY_TASK",1024*5,NULL,3,NULL);
    

    // while(1)
    // {
    //     WAV_PLAY();
    //     vTaskDelay(5000/portTICK_RATE_MS);

    // }
    return 0;
}
